package com.mapaoer.user.service.impl;

import com.mapaoer.user.dao.SkillTagRepository;
import com.mapaoer.user.entity.SkillTag;
import com.mapaower.user.service.SkillTagService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 技能标签服务实现类
 * 实现SkillTagService接口定义的技能标签相关业务操作
 */
@Service
public class SkillTagServiceImpl implements SkillTagService {

    @Autowired
    private SkillTagRepository skillTagRepository;

    @Override
    @Transactional
    public SkillTag createSkillTag(String name, String category, String color, Boolean isHot) {
        // 检查技能标签是否已存在
        SkillTag existingTag = skillTagRepository.selectOne(new QueryWrapper<SkillTag>().eq("name", name));
        if (existingTag != null) {
            return null;
        }

        SkillTag skillTag = new SkillTag();
        skillTag.setName(name);
        skillTag.setCategory(SkillTag.SkillCategory.valueOf(category));
        skillTag.setColor(color != null ? color : "#007bff");
        skillTag.setIsHot(isHot != null ? isHot : false);
        skillTag.setCreateTime(LocalDateTime.now());
        skillTag.setUpdateTime(LocalDateTime.now());

        skillTagRepository.insert(skillTag);
        return skillTag;
    }

    @Override
    public SkillTag getSkillTagById(Long id) {
        SkillTag skillTag = skillTagRepository.selectById(id);
    return skillTag;
    }

    @Override
    public SkillTag getSkillTagByName(String name) {
        SkillTag skillTag = skillTagRepository.selectOne(new QueryWrapper<SkillTag>().eq("name", name));
        return skillTag;
    }

    @Override
    public List<SkillTag> getSkillTagsByCategory(String category) {
        return skillTagRepository.selectList(new QueryWrapper<SkillTag>().eq("category", SkillTag.SkillCategory.valueOf(category)));
    }

    @Override
    public List<SkillTag> getAllSkillTags() {
        return skillTagRepository.selectList(new QueryWrapper<>());
    }

    @Override
    public List<SkillTag> getHotSkillTags() {
        return skillTagRepository.selectList(new QueryWrapper<SkillTag>().eq("is_hot", true));
    }

    @Override
    @Transactional
    public SkillTag updateSkillTag(Long id, String name, String category, String color, Boolean isHot) {
        SkillTag skillTag = skillTagRepository.selectById(id);
        if (skillTag == null) {
            return null;
        }
        
        if (name != null) {
            skillTag.setName(name);
        }
        
        if (category != null) {
            skillTag.setCategory(SkillTag.SkillCategory.valueOf(category));
        }
        
        if (color != null) {
            skillTag.setColor(color);
        }
        
        if (isHot != null) {
            skillTag.setIsHot(isHot);
        }
        
        skillTag.setUpdateTime(LocalDateTime.now());
        
        skillTagRepository.updateById(skillTag);
        return skillTag;
    }

    @Override
    @Transactional
    public boolean deleteSkillTag(Long id) {
        SkillTag skillTag = skillTagRepository.selectById(id);
        if (skillTag == null) {
            return false;
        }

        skillTagRepository.deleteById(id);
        return true;
    }
}